using UnityEngine;
using System.Collections;

public class PlayerSound : MonoBehaviour 
{
	
	public AudioClip[] JumpClip;
	private int lLastGrabbed;
	
	public AudioClip[] LandClip;
	private int lLastLandGrabbed;
	
	public AudioClip[] FiringClip;
	private int lLastFireGrabbed;
	
	public AudioClip HurtClip;
	
	public AudioClip[] PowerUpClip;
	private int lLastPowerupGrabbed;
	
	public AudioClip BombUpClip;
	
	public AudioClip DeathClip;
	
	public AudioClip BombUseClip;
	
	//public AudioClip ShieldClip;
	
	private AudioSource jumpingSource;
	private AudioSource fallingSource;
	private AudioSource firingSource;
	private AudioSource hurtSource;
	private AudioSource powerUpSource;
	
	private AudioSource bombeUseSource;
	
	private GameObject SoloGO;
	
	// Use this for initialization
	void Start () 
	{
		SoloGO = new GameObject();
		SoloGO.transform.parent = transform;
		
		jumpingSource = gameObject.AddComponent<AudioSource>();
		fallingSource = gameObject.AddComponent<AudioSource>();
		firingSource = gameObject.AddComponent<AudioSource>();
		hurtSource = SoloGO.gameObject.AddComponent<AudioSource>();
		powerUpSource = gameObject.AddComponent<AudioSource>();
		
		bombeUseSource = gameObject.AddComponent<AudioSource>();
		
		/*shieldSource = gameObject.AddComponent<AudioSource>();
		shieldSource.clip = ShieldClip;
		shieldSource.loop = true;*/
		
		lLastGrabbed = -1;
		lLastLandGrabbed = -1;
		lLastFireGrabbed = -1;
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	
	public void OnJump()
	{
		int lRand = Random.Range(0, JumpClip.Length);
		
		if(lRand == lLastGrabbed)
		{
			lRand++;
			if(lRand >= JumpClip.Length)
				lRand = 0;
		}
		
		if(!jumpingSource.isPlaying)
			jumpingSource.PlayOneShot(JumpClip[lRand]);
		
		lLastGrabbed = lRand;
	}
	
	public void OnDie()
	{
		SoloGO.transform.parent = null;
		SoloGO.AddComponent<AutoDestruct>().SetSuicide(3.0f);
		hurtSource.priority = 255;
		hurtSource.volume = 2;
		hurtSource.PlayOneShot(DeathClip);
	}
	
	public void OnLand()
	{
		int lRand = Random.Range(0, LandClip.Length);
		
		if(lRand == lLastLandGrabbed)
		{
			lRand++;
			if(lRand >= LandClip.Length)
				lRand = 0;
		}
		
		fallingSource.PlayOneShot(LandClip[lRand]);
		
		lLastLandGrabbed = lRand;
	}
	
	public void OnFire()
	{
		int lRand = Random.Range(0, FiringClip.Length);
		
		if(lRand == lLastFireGrabbed)
		{
			lRand++;
			if(lRand >= FiringClip.Length)
				lRand = 0;
		}
		
		firingSource.PlayOneShot(FiringClip[lRand]);
		
		lLastFireGrabbed = lRand;
	}
	
	public void OnBombUse()
	{
		bombeUseSource.PlayOneShot(BombUseClip);	
	}
	
	public void OnHit()
	{
		hurtSource.PlayOneShot(HurtClip);
	}
	
	public void OnPowerUp()
	{
		int lRand = Random.Range(0, PowerUpClip.Length);
		
		if(lRand == lLastPowerupGrabbed)
		{
			lRand++;
			if(lRand >= PowerUpClip.Length)
				lRand = 0;
		}
		
		powerUpSource.PlayOneShot(PowerUpClip[lRand]);
		
		lLastPowerupGrabbed = lRand;
	}
	
	public void OnBombGrabbed()
	{
		powerUpSource.PlayOneShot(BombUpClip);
	}
	
	/*public void OnShieldUp()
	{
		shieldSource.Play();
	}
	
	public void OnShieldDown()
	{
		shieldSource.Stop();
	}*/
}
